package com.heima.commnet.service.Impl;

import com.heima.commnet.service.CommentRepayService;
import com.heima.common.exception.CustException;
import com.heima.feign.UserFeign;
import com.heima.model.comment.dto.CommentRepayDTO;
import com.heima.model.comment.dto.CommentRepayLikeDTO;
import com.heima.model.comment.dto.CommentRepaySaveDTO;
import com.heima.model.comment.pojos.ApComment;
import com.heima.model.comment.pojos.ApCommentRepay;
import com.heima.model.comment.pojos.ApCommentRepayLike;
import com.heima.model.comment.vo.ApCommentRepayVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CommentRepayServiceImpl implements CommentRepayService {
  @Autowired UserFeign userFeign;
  @Autowired MongoTemplate mongoTemplate;

  /**
   * 加载回复评论列表
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult loadCommentRepay(CommentRepayDTO dto) {
    // 1. 校验参数
    if (dto.getSize() == null || dto.getSize() <= 0) {
      dto.setSize(10);
    }
    if (dto.getMinDate() == null) {
      dto.setMinDate(new Date());
    }
    // 2. 查询评论回复列表   commentId = ?  and  createdTime < minDate  排序 createdTime desc  limit size
    Query query =
        Query.query(
                Criteria.where("commentId")
                    .is(dto.getCommentId())
                    .and("createdTime")
                    .lt(dto.getMinDate()))
            .limit(dto.getSize())
            .with(Sort.by(Sort.Direction.DESC, "createdTime"));
    List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query, ApCommentRepay.class);
    // 3. 是否登录
    ApUser user = AppThreadLocalUtils.getUser();
    // 3.1 如果未登录  或者 回复的集合是空的  那么直接返回集合
    if (user == null || apCommentRepays == null || apCommentRepays.size() == 0) {
      return ResponseResult.okResult(apCommentRepays);
    }
    // 3.2 如果登录了  并且 集合不为空 判断 当前回复列表中 哪些回复点过赞
    List<String> commentRepayIds =
        apCommentRepays.stream().map(ApCommentRepay::getId).collect(Collectors.toList());
    // 用户id = user.getId    commentRepayId in 集合
    Query likedCommentRepay =
        Query.query(
            Criteria.where("authorId").is(user.getId()).and("commentRepayId").in(commentRepayIds));
    // 得到当前回复列表中 ，点过赞的集合
    List<ApCommentRepayLike> apCommentRepayLikes =
        mongoTemplate.find(likedCommentRepay, ApCommentRepayLike.class);
    if (apCommentRepayLikes != null && apCommentRepayLikes.size() > 0) {
      List<String> likedCommentRepayIds =
          apCommentRepayLikes.stream()
              .map(ApCommentRepayLike::getCommentRepayId)
              .collect(Collectors.toList());
      // 说明有点过赞的回复
      List<ApCommentRepayVO> apCommentRepayVoList = new ArrayList<>();
      apCommentRepays.forEach(
          apCommentRepay -> {
            ApCommentRepayVO apCommentRepayVo = new ApCommentRepayVO();
            BeanUtils.copyProperties(apCommentRepay, apCommentRepayVo);
            if (likedCommentRepayIds.contains(apCommentRepay.getId())) {
              apCommentRepayVo.setOperation((short) 0);
            }
            apCommentRepayVoList.add(apCommentRepayVo);
          });
      return ResponseResult.okResult(apCommentRepayVoList);
    }
    return ResponseResult.okResult(apCommentRepays);
  }

  /**
   * 保存回复评论
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult saveCommentRepay(CommentRepaySaveDTO dto) {
    ApUser user = AppThreadLocalUtils.getUser();
    if (user == null) {
      CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
    }
    //获取user信息
    ResponseResult<ApUser> userResult = userFeign.findUserById(user.getId());
    if (userResult.getCode() != 0) {
      CustException.cust(AppHttpCodeEnum.SERVER_ERROR, userResult.getErrorMessage());
    }
    user = userResult.getData();
    if (user == null) {
      CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "登录信息有误");
    }
    //保存评论
    ApCommentRepay apCommentRepay = new ApCommentRepay();

    apCommentRepay.setAuthorId(user.getId());
    apCommentRepay.setAuthorName(user.getName());
    apCommentRepay.setCommentId(dto.getCommentId());
    apCommentRepay.setContent(dto.getContent());
    apCommentRepay.setLikes(0);

    apCommentRepay.setCreatedTime(new Date());
    apCommentRepay.setUpdatedTime(new Date());
    mongoTemplate.insert(apCommentRepay);
    // 更新评论的数量
    ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
    apComment.setReply(apComment.getReply() + 1);
    mongoTemplate.save(apComment);

    return ResponseResult.okResult();
  }

  /**
   * 保存回复评论数
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult saveCommentRepayLike(CommentRepayLikeDTO dto) {
      ApUser user = AppThreadLocalUtils.getUser();
      if (user == null) {
          CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
      }
      //点赞操作
      ApCommentRepay apCommentRepay = mongoTemplate.findById(dto.getCommentRepayId(), ApCommentRepay.class);
      if (apCommentRepay==null){
          return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
      }
      Query query = Query.query(Criteria.where("authorId").is(user.getId()).and("commentRepayId").is(apCommentRepay.getId()));
      ApCommentRepayLike commentRepayLike = mongoTemplate.findOne(query, ApCommentRepayLike.class);
      if (dto.getOperation()==0){
          if (commentRepayLike!=null){
              CustException.cust(AppHttpCodeEnum.DATA_EXIST,"已经点赞过");
          }
          //更新点赞数
          apCommentRepay.setLikes(apCommentRepay.getLikes()+1);
          mongoTemplate.save(apCommentRepay);
          //保存对应评论的信息
          ApCommentRepayLike apCommentRepayLike = new ApCommentRepayLike();
          apCommentRepayLike.setAuthorId(user.getId());
          apCommentRepayLike.setCommentRepayId(dto.getCommentRepayId());
          apCommentRepayLike.setOperation(dto.getOperation());
          mongoTemplate.save(apCommentRepayLike);
      }else {
          if (commentRepayLike==null){
              CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"还没有点赞");
          }
          apCommentRepay.setLikes(apCommentRepay.getLikes()<0?0:apCommentRepay.getLikes()-1);
          mongoTemplate.save(apCommentRepay);
          mongoTemplate.remove(query,ApCommentRepayLike.class);
      }

      //5.数据返回
      Map<String, Object> resultMap = new HashMap<>();
      resultMap.put("likes",apCommentRepay.getLikes());
      return ResponseResult.okResult(resultMap);
  }
}
